<template>
  <div class="container">
    <div class="login-card">
      <div style="text-align: center; font-size: 24px; margin-bottom: 30px; color: #333">欢迎登录博客平台</div>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="username">
          <el-input size="medium" prefix-icon="el-icon-user" placeholder="请输入账号" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" prefix-icon="el-icon-lock" placeholder="请输入密码" show-password  v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item prop="role">
          <el-radio-group v-model="form.role">
            <el-radio label="USER">用户</el-radio>
            <el-radio label="ADMIN">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="code">
          <div style="display: flex;">
            <el-input style="flex: 1" size="medium" v-model="code"></el-input>
            <Identify :identifyCode="identifyCode" @click.native="refreshCode"/>
          </div>
        </el-form-item>
        <el-form-item>
          <div style="display: flex; justify-content: space-between">
            <el-button size="medium" class="login-button" @click="login">登 录</el-button>
            <el-button size="medium" class="login-go" style="margin-right: 10px" @click="go">返 回 首 页</el-button>
          </div>
        </el-form-item>
        <div style="display: flex; align-items: center">
          <div style="flex: 1"></div>
          <div style="flex: 1; text-align: right">
            还没有账号？请 <a href="/register">注册</a>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import Identify from "@/components/Identify";
import router from "@/router";

export default {
  name: "Login",
  components: {
    Identify
  },
  data() {
    return {
      form: { role: 'USER' },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      },
      code:'',     //表单绑定的验证码属性
      //图片验证码
      identifyCode: '',
      //验证码规则
      identifyCodes: '123456789ABCDEFGHJKMNPQRSTUVWXYZ',
    }
  },
  mounted() {
    this.refreshCode()
  },
  created() {

  },
  methods: {
    router() {
      return router
    },
    //切换验证码
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes,4)   //验证码的位数
    },
    //生成随机验证码
    makeCode(o,l) {
      for(let i = 0;i < l;i++) {
        this.identifyCode += this.identifyCodes[Math.floor(Math.random() * (this.identifyCodes.length))]
      }
    },
    login() {
      if(!this.code) {
        this.$message.warning("请输入验证码")
        return
      }
      if(this.code.toLowerCase() !== this.identifyCode.toLowerCase()) {   //都转成小写再比较
        this.$message.warning("验证码错误")
        this.refreshCode()
        return
      }
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/login', this.form).then(res => {
            if (res.code === '200') {
              localStorage.setItem("weblog", JSON.stringify(res.data))  // 存储用户数据
              setTimeout(() => {
                this.$message.success('登录成功')
                //跳转页面
                if (res.data.role === 'ADMIN') {
                  location.href = '/home'
                } else {
                  location.href = '/weblog/home'  // 跳转主页
                }
              },500)
            } else {
              this.refreshCode()
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    go() {
      location.href = '/'
    }
  }
}
</script>

<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/imgs/login.png");
  background-repeat: no-repeat;
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}

.login-card {
  width: 380px;
  padding:50px 30px;
  background-color: white;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  opacity: 0.75;
}
a {
  color: #2a60c9;
}

.login-button {
  width: 100%;
  background-color: #2A60C9;
  border-color: #2A60C9;
  color: white;
}

.login-go {
  width: 100%;
  background-color: #00b4b3;
  border-color: #00b4b3;
  color: white;
}
</style>